package com.lsq.tucao.express.service.serviceImpl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsq.tucao.express.bean.po.HeartDiscuss;
import com.lsq.tucao.express.dao.HeartDetailMapper;
import com.lsq.tucao.express.dao.HeartDiscussMapper;
import com.lsq.tucao.express.response.JsonResult;
import com.lsq.tucao.express.service.HeartDiscussService;
import com.lsq.tucao.express.utils.SensitiveWordFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class HeartDiscussServiceImpl implements HeartDiscussService {

    @Autowired
    private HeartDiscussMapper heartDiscussMapper;
    @Autowired
    private HeartDetailMapper heartDetailMapper;
    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    @Override
    public JsonResult getPageDiscussByHeartId(Integer current, Integer size, Long heartId) {
        if (heartId != null) {
            Page<HeartDiscuss> page = new Page<>(current, size);
            QueryWrapper<HeartDiscuss> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(HeartDiscuss::getHeartId, heartId)
                    .orderByDesc(HeartDiscuss::getCreateTime);

            Page<HeartDiscuss> heartDiscussPage = heartDiscussMapper.selectPage(page, queryWrapper);
            if (heartDiscussPage != null) {
                return new JsonResult(heartDiscussPage, 200, "分页查询鸡汤评论数据成功");
            } else {
                return new JsonResult(500, "分页查询鸡汤评论数据失败");
            }
        } else {
            return new JsonResult(500, "请传入有效的heartId");
        }
    }

    @Override
    public JsonResult addDiscuss(HeartDiscuss heartDiscuss) {
        if (heartDiscuss != null && heartDiscuss.getHeartId() != null && !StringUtils.isEmpty(heartDiscuss.getContent())) {
            //检测发布的吐槽中是否包含敏感词汇
            Set<String> set = sensitiveWordFilter.getSensitiveWord(heartDiscuss.getContent(), 1);
            if (set.size() > 0) {
                return new JsonResult(500, "发布的评论中含有违禁词" + set);
            } else {
                int update = heartDetailMapper.discussHeart(heartDiscuss.getHeartId());
                int insert = heartDiscussMapper.insert(heartDiscuss);
                if (insert > 0 && update > 0) {
                    return new JsonResult(200, "发布评论成功");
                } else {
                    return new JsonResult(500, "发布评论失败");
                }
            }

        } else {
            return new JsonResult(500, "请输入要发表的评论内容");
        }
    }
}
